//-----------------------------------------------------------------------
// <copyright file="GalleryDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class GalleryDAOBase
    {
        #region Common methods
        public virtual Gallery CreateGalleryFromReader(IDataReader reader)
        {
            Gallery item = new Gallery();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("GalleryId"))) item.GalleryId = (long)reader["GalleryId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Image"))) item.Image = (string)reader["Image"];
                if (!reader.IsDBNull(reader.GetOrdinal("ThumbnailImage"))) item.ThumbnailImage = (string)reader["ThumbnailImage"];
                if (!reader.IsDBNull(reader.GetOrdinal("TGKM_ObjectId"))) item.TGKM_ObjectId = (long)reader["TGKM_ObjectId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateGalleryFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateGallery methods
            
        public virtual void CreateGallery(Gallery gallery)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGalleryInsert");
                
                database.AddInParameter(dbCommand, "@Image", DbType.String, gallery.Image);
                database.AddInParameter(dbCommand, "@ThumbnailImage", DbType.String, gallery.ThumbnailImage);
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, gallery.TGKM_ObjectId);
                database.AddOutParameter(dbCommand, "@GalleryId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                gallery.GalleryId = (long)database.GetParameterValue(dbCommand, "@GalleryId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateGalleryException, ex);
            }
        }

        #endregion

        #region UpdateGallery methods
        
        public virtual void UpdateGallery(Gallery gallery)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGalleryUpdate");            
                
                database.AddInParameter(dbCommand, "@GalleryId", DbType.Int64, gallery.GalleryId);
                database.AddInParameter(dbCommand, "@Image", DbType.String, gallery.Image);
                database.AddInParameter(dbCommand, "@ThumbnailImage", DbType.String, gallery.ThumbnailImage);
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, gallery.TGKM_ObjectId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateGalleryException, ex);
            }
        }
        
        #endregion

        #region DeleteGallery methods
        public virtual void DeleteGallery(long galleryId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGalleryDelete");
                
                database.AddInParameter(dbCommand, "@GalleryId", DbType.Int64, galleryId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteGalleryException, ex);
            }
        }

        #endregion

        #region GetGallery methods
        
        public virtual Gallery GetGallery(long galleryId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGalleryGet");
                
                database.AddInParameter(dbCommand, "@GalleryId", DbType.Int64, galleryId);
                
                Gallery gallery =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        gallery = CreateGalleryFromReader(reader);
                        reader.Close();
                    }
                }
                return gallery;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetGalleryException, ex);
            }
        }
        
        #endregion

        #region GetGalleryList methods
        public virtual GalleryCollection GetGalleryList(GalleryColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGalleryGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                GalleryCollection galleryCollection = new GalleryCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Gallery gallery = CreateGalleryFromReader(reader);
                        galleryCollection.Add(gallery);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return galleryCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetGalleryListException, ex);
            }
        }
        
        public virtual GalleryCollection GetGalleryList(GalleryColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetGalleryList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetGalleryList methods
        public virtual GalleryCollection GetGalleryListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGalleryGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                GalleryCollection galleryCollection = new GalleryCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Gallery gallery = CreateGalleryFromReader(reader);
                        galleryCollection.Add(gallery);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return galleryCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetGalleryListException, ex);
            }
        }
        
        public virtual GalleryCollection GetGalleryListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetGalleryListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}